package com.bonc.ioc.service.impl;

import com.bonc.ioc.base.page.PageData;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.DataAssetDao;
import com.bonc.ioc.dao.TopicMapper;
import com.bonc.ioc.model.DataAssetInfo;
import com.bonc.ioc.service.TopicService;
import com.bonc.ioc.vo.KnowledgesVo;
import com.bonc.ioc.vo.TopicVo;
import com.bonc.ioc.vo.UpTopicVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindException;

import javax.annotation.Resource;
import java.util.*;

/**
 * @version 1.0
 * @description:
 * @author: wangqinglin
 * @date: 2021/3/18 11:06
 * @change : 2021/3/18 11:06 by q8541q@163.com  for init
 */
@Service
public class TopicServiceImpl extends BaseService implements TopicService {

    @Resource
    private TopicMapper topicMapper;

    @Resource
    private DataAssetDao dataAssetDao;

    @Override
    public int deleteByPrimaryKey(String id) {
        return topicMapper.delete(id);
    }

    @Override
    public int save(TopicVo record) {
        if (record.getId() != null) {
            fillModifyTimeInfo(record);
            return topicMapper.update(record);
        } else {
            fillBasicTimeInfo(record);
            fillModifyTimeInfo(record);
            return topicMapper.save(record);
        }
    }

    @Override
    public TopicVo selectByPrimaryKey(Long id) {
        return topicMapper.selectByPrimaryKey(id);
    }


    @Override
    public PageResult selectTopicInfoByPage(PageData pageData) throws BindException {
        getPageParams();
        PageResult pageResult = new PageResult(pageData, topicMapper.selectByPage(pageData));
        return pageResult;
    }

    @Override
    public PageResult selectTopicByPage(HashMap<String, Object> map) throws BindException {
        HashMap<String, Object> pageParams = this.getPageParams();
        map.putAll(pageParams);
        List<UpTopicVo> topicVos = topicMapper.selectTopicByPage(map);
        for (UpTopicVo topicVo : topicVos) {
            if (CollectionUtils.isEmpty(topicVo.getTitles())) {
                topicVo.setDataNumber(0);
            } else {
                topicVo.setDataNumber(topicVo.getTitles().size());
            }

            if (CollectionUtils.isEmpty(topicVo.getKnowledgeTitle())) {
                topicVo.setKnowledgeTitleNumber(0);
            } else {
                topicVo.setKnowledgeTitleNumber(topicVo.getKnowledgeTitle().size());
            }
        }
        return new PageResult(map, topicVos);
    }

    @Override
    public Map<String, Object> selectTopicMessage(String id) {
        //查询专题详情
        TopicVo topicVo = topicMapper.selectTopicMessage(id);

        //查询对应的科研数据标题
        String resourceList = topicVo.getResourceList();
        List<String> rList = splitIds(resourceList);
        List<DataAssetInfo> dataAssetInfos = new LinkedList<>();
        for (String dataId : rList) {
            DataAssetInfo dataAssetInfo = dataAssetDao.selectDataDetailById(Long.valueOf(dataId));
            if (dataAssetInfo != null) {
                dataAssetInfos.add(dataAssetDao.selectDataDetailById(Long.valueOf(dataId)));
            }
        }

        //查询对应的科研软件的标题
        String knowledgeList = topicVo.getKnowledgeList();
        List<KnowledgesVo> knowledgesVos = new ArrayList<>();
        List<String> kList = splitIds(knowledgeList);
        if (!CollectionUtils.isEmpty(kList)) {
            knowledgesVos = topicMapper.selectKnowledgesVo(kList);
        }

        topicVo.setResourceSize(dataAssetInfos.size());
        topicVo.setKnowledgeSize(knowledgesVos.size());

        //访问量加1
        topicMapper.updateTopicViews(id);
        Map<String, Object> map = new HashMap<>();
        map.put("topicVo", topicVo);
        map.put("dataAssetInfos", dataAssetInfos);
        map.put("knowledgesVos", knowledgesVos);
        return map;
    }

    private List<String> splitIds(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        String[] splits = ids.split(",");
        return new ArrayList<>(Arrays.asList(splits));
    }

    private StringBuilder strAppend(List<String> titles) {
        if (CollectionUtils.isEmpty(titles)) {
            return new StringBuilder();
        }
        StringBuilder builder = new StringBuilder();
        for (String title : titles) {
            builder.append(title).append(",");
        }
        builder.deleteCharAt(builder.length() - 1);
        return builder;
    }

    private void fillModifyTimeInfo(TopicVo record) {
        record.setModifyTime(new Date());
    }

    private void fillBasicTimeInfo(TopicVo record) {
        record.setCreateTime(new Date());
    }
}
